from flask import Flask,jsonify,render_template,request,escape
from gaode import geocode,around
import pandas as pd

app = Flask(__name__)

# 调用日志记录函数
def log_request(req:'flask_request',res:str)->None:
	with open('view.log','a') as log:
		print(req.form,req.remote_addr,req.user_agent,res,file=log,sep='|')

# 进入网页的首页，数据返回gongnen1.html
@app.route('/base')
def entry_page() ->'html':
	return render_template('gongnen1.html',the_title='高高咯搜索POI服务')


# 点击“搜索”后，调用高德api——geocode和around，数据返回gongnen2
@app.route('/search',methods=['POST'])
def go_search():
	address = request.form['address']
	codes = str(geocode(address)['geocodes'][0][ 'location'])

	location = codes
	types = request.form['types']
	results = around(location,types)['pois']


	names = []
	address2 = []
	for i in results:
		names.append(i['name'])
		address2.append(i['address'])
	combine = dict(zip(names,address2))

	log_request(request,results)
	return render_template('gongnen2.html',res=combine,the_title='为您返回的结果：')
	


# 查看日志
@app.route('/viewlog')
def view_the_log() ->'html':
	with open('view.log',"r") as log:
		contents = []
		for line in log:
			contents.append([])
			for item in line.split('|'):
				contents[-1].append(escape(item))

	titles = ('Form data','Remote_addr','User_agent','results')
	return render_template('viewlog.html',
		the_title='View Log',
		the_row_titles=titles,
		the_data=contents,)

if __name__=='__main__':
	app.run(debug=True)